var common = require('../common');
var model = require('../../../model/model');
var rooms = require('../../../model/rooms');
var utils = require('../../../util/utils');
var logger = require('pomelo-logger').getLogger('fightHandler',__filename);
module.exports = function(app)
{
    return new Handler(app);
};
var Handler = function(app)
{
    this.app = app;
};

var handler = Handler.prototype;

handler.fightMatch = function(msg , session ,next)
{
    var actorId = msg.actorId;
    var roomId = msg.roomId || 0;
    var uid = session.uid;
    var time = utils.getTime();
    var self = this;
    var me =
    {
        'uid':uid,
        'aid':actorId,
        'roomId':roomId,
        'isReady':0,
        'time':time
    };
    model.getActorExp(me,function(exp)
    {
        me.exp = exp;
        session.set('me',me);
        session.pushAll();
        rooms.addPlayer(me);
        self.app.rpc.fight.fightRemote.doMatch(session,function()
        {

        })
        next();
    })
};

handler.fightReady = function(msg ,session ,next)
{
    var player = session.get('me');
    var table = session.get('table');
    if( table )
    {
        player.isReady = 1;
        rooms.updateTable(player,function(res)
        {
            session.set('table',res);
            session.pushAll();
            var isRobot = res.isRobot;
            if( isRobot )
            {
                console.info('enemy is a robot would not send onReady message');
            }
            else
            {
                var eid = common.findEnemyByTable(player,table).uid;
                common.sendEvent(msg,eid,'onReady');
            }
        });
    }
    else
    {
        console.info('fight ready table not exist');
    }
    next();
};

handler.reMatch = function(msg ,session,next)
{
    var me = session.get('me');
    me.time = utils.getTime();
    this.app.rpc.fight.fightRemote.doRematch(session,me,function()
    {

    })
    next();
}

handler.fightLeave = function(msg ,session,next)
{
    var player = session.get('me');
    var table = session.get('table');
    console.info("fight leave");
    this.app.rpc.fight.fightRemote.fightLeave(session,player,table,function()
    {
        session.set('me',0);
        session.set('table',0);
        session.pushAll();
    })
    next();
};

handler.fightEscape = function(msg , session ,next)
{
    var table = session.get('table');
    var me = session.get('me');
    if( table )
    {
        this.app.rpc.fight.fightRemote.fightEscape(session,me,table,function()
        {
            session.set('me',0);
            session.set('table',0);
            session.set('inFight',0);
            session.pushAll();
        })
    }
    else
    {
        console.info('fight escape table not exist');
    }
    next();
};

handler.skillUse = function(msg ,session ,next)
{
    var player = session.get('me');
    var table = session.get('table');
    if( table )
    {
        var eid = common.findEnemyByTable(player,table).uid;
        common.sendEvent(msg,eid,'onSkill');
    }
    else
    {
        console.info('skill use table not exist');
    }
    next();
};

handler.pieceUse = function(msg,session ,next)
{
    var player = session.get('me');
    var table = session.get('table');
    if( table )
    {
        var eid = common.findEnemyByTable(player,table).uid;
        common.sendEvent(msg,eid,'onPiece');
    }
    next();
};

handler.matrixSync = function(msg , session ,next)
{
    var player = session.get('me');
    var table = session.get('table');
    if ( table )
    {
        session.set('inFight',1);
        session.pushAll();
        var eid = common.findEnemyByTable(player,table).uid;
        common.sendEvent(msg,eid,'onMatrixSync');
    }
    else
    {
        console.info('matrix sync table not exist');
    }
    next();
};

handler.matrixReq = function(msg ,session,next)
{
    next();
};

handler.pieceMove =  function(msg , session ,next)
{
    var me = session.get('me');
    var table = session.get('table');
    if( table )
    {
        var eid = common.findEnemyByTable(me,table).uid;
        common.sendEvent(msg,eid,'onMove');
    }
    else
    {
        console.info('piece move table not exist');
    }
    next();
};

handler.fightResult = function(msg ,session,next)
{
    var player = session.get('me');
    var result = msg.result;
    var table = session.get('table');
    this.app.rpc.fight.fightRemote.doResult(session,player,false,table,result,function()
    {
        session.set('me',0);
        session.set('table',0);
        session.set('inFight',0);
        session.pushAll();
    })
    next();
};